<html>
<head>

</head>
<body onload="init()">
<object id="pluginobj" type="application/x-npdownload" hidden="true">
Failed to load np plugin. You might be using the old version of the extension.</object>

<script>

var TEMP;
var js;
var link;
var allLinks;
var pluginobj = document.getElementById('pluginobj');


// When using experimental API, change this flag to true, and add
// an "experimental" permission in manifest.json
var useExperimentalAPI = false;

// Check if downloader is enabled

function scanDownloader() {
  localStorage.flashget = checkObjectByNP('FlashGet');
  localStorage.miniFlashget = checkObjectByNP('miniFlashget');
  localStorage.thunder = checkObjectByNP('ThunderAgent.Agent.1');
  localStorage.miniThunder = checkObjectByNP('ToolbarThunder.DownloadAgent.1');

}

function checkObjectByNP(progID) {
  return pluginobj.CheckObject(progID);
}

function setPopupStatus() {
  scanDownloader();
  chrome.browserAction.setTitle({title: chrome.i18n.getMessage('tipUnable')});
  chrome.browserAction.setIcon(
      {path: chrome.extension.getURL('icon_19_disable.png')});
  chrome.browserAction.setPopup({popup: ''});
  chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.executeScript(tab.id, {code: 'checkLinks()'}, function() {
      chrome.extension.onRequest.addListener(function(request, sender, response) {
        var browserActionTitle;
        var setBrowserActionIcon = 'icon_19_disable.png';
        var setPopupUrl = '';
        if (request && request.msg == 'noLinks') {
          browserActionTitle = chrome.i18n.getMessage('tipNolink');
        } else if (request.msg == 'haveLinks') {
          setBrowserActionIcon = 'icon_19.png';
          var count = 0;
          if (localStorage.thunder == 'true') {
            count++;
            browserActionTitle = chrome.i18n.getMessage('downloadAllByThunder');
          }
          if (localStorage.flashget == 'true') {
            count++;
            browserActionTitle = chrome.i18n.getMessage('downloadAllByFlashget');
          }
          if (count == 2) {
            browserActionTitle = chrome.i18n.getMessage('downloadAllByThunderAndFlashget');
            setPopupUrl = 'popup.html';
          } else if (count == 0) {
            setBrowserActionIcon = 'icon_19_disable.png';
            if (localStorage.miniFlashget == 'false' && localStorage.miniThunder == 'false') {
              browserActionTitle = chrome.i18n.getMessage('noDownloader');
            } else if (localStorage.miniFlashget == 'true' && localStorage.miniThunder == 'false') {
              browserActionTitle = chrome.i18n.getMessage('noDownloader2');
            } else if (localStorage.miniFlashget == 'false' && localStorage.miniThunder == 'true') {
              browserActionTitle = chrome.i18n.getMessage('noDownloader3');
            } else if (localStorage.miniFlashget == 'true' && localStorage.miniThunder == 'true') {
              browserActionTitle = chrome.i18n.getMessage('noDownloader4');
            }
            chrome.browserAction.setIcon(
                {path: chrome.extension.getURL('icon_19_disable.png')});
          }
        }
        chrome.browserAction.setTitle({title: browserActionTitle});
        chrome.browserAction.setIcon(
            {path: chrome.extension.getURL(setBrowserActionIcon)});
        chrome.browserAction.setPopup({popup: setPopupUrl});
      });
    });
  });
}
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  setPopupStatus();
});

chrome.browserAction.onClicked.addListener(function(tab) {
  if (localStorage.thunder == 'true' && localStorage.flashget == 'false') {
      chrome.tabs.executeScript(tab.id,
          {code: 'sendDownloadCommandToBg("ByThunder")'});
    } else if (localStorage.thunder == 'false' && localStorage.flashget == 'true') {
      chrome.tabs.executeScript(tab.id,
          {code: 'sendDownloadCommandToBg("ByFlashget")'});
    }
});

function addMyItemToContextMenu() {
  var downloaderName;
  if (eval(localStorage.flashget)) {
    downloaderName = chrome.i18n.getMessage('mFlashget');
    chrome.experimental.contextMenu.create(
        {'title': downloaderName,
         'contexts': ['LINK'],
         'onclick': function(obj){
           pluginobj.flashgetAddLink(obj.linkUrl, '', obj.mainFrameUrl);
         }});
    downloaderName = chrome.i18n.getMessage('downloadAllByFlashget');
    chrome.experimental.contextMenu.create(
      {'title': downloaderName,
       'contexts': ['ALL'],
       'onclick': function(obj) {
         if (obj.frameUrl) {
           chrome.tabs.executeScript(null, {code: 'downloadAll("' + obj.frameUrl + '", "flashget")', allFrames: true})
         } else {
           chrome.tabs.executeScript(null,
               {code: 'sendDownloadCommandToBg("ByFlashget");'})
         }
       }});
  }
  if (eval(localStorage.thunder)) {
    downloaderName = chrome.i18n.getMessage('mThunder');
    chrome.experimental.contextMenu.create(
        {'title': downloaderName,
         'contexts': ['LINK'],
         'onclick': function(obj) {
           pluginobj.thunderAddLink(obj.linkUrl, '', obj.mainFrameUrl);
         }});

    downloaderName = chrome.i18n.getMessage('downloadAllByThunder');
    chrome.experimental.contextMenu.create(
      {'title': downloaderName,
       'contexts': ['ALL'],
       'onclick': function(obj) {
         if (obj.frameUrl) {
           chrome.tabs.executeScript(null, {code: 'downloadAll("' + obj.frameUrl + '", "thunder")', allFrames: true})
         } else {
           chrome.tabs.executeScript(null,
               {code: 'sendDownloadCommandToBg("ByThunder");'})
         }
       }});

    downloaderName = chrome.i18n.getMessage('option');
    chrome.experimental.contextMenu.create(
      {'title': downloaderName,
       'contexts': ['ALL'],
       'onclick': function() {
            chrome.tabs.create({'url': 'options.html'})
            }});
  }
}

function downloadLinkByFlashgetOrMiniFlashget(link, downloader) {
  var flashgetAgent = pluginobj.CreateObject(downloader);
  flashgetAgent.AddLink(link[0], link[1], link[2]);
}

function downloadLinkByThunder(link) {
  var thunderAgent = pluginobj.CreateObject('ThunderAgent.Agent.1');
  thunderAgent.AddTask5(link[0], '', '', '', '',
      -1, 0, -1,  '', '', '', 1, '', -1);
  thunderAgent.CommitTasks2(1);
}

function downloadLinkByMiniThunder(link) {
  var miniThunderAgent = pluginobj.CreateObject('ToolbarThunder.DownloadAgent.1');
  miniThunderAgent.AddTask(link[0], '', '', '', '');
}

function downloadLinkByWebThunder(link) {
  var webThunderAgent = pluginobj.CreateObject('ThunderServer.WebThunder.1');
  webThunderAgent.CallAddTask2(link[0], '', '', 2, '', '', '');
}

function copyLinkToClipboard(url) {
  pluginobj.CopyToClipboard(url);
}

function init() {
  chrome.tabs.onSelectionChanged.addListener(function(tabId) {

    js = 'chrome.tabs.executeScript(' + tabId + ', {file: "npdownload.js"});';
    chrome.tabs.sendRequest(tabId, {
        'msg': 'init_check',
        'downloader': localStorage.downloader,
        'contextmenu': localStorage.contextmenu,
        'flashget': localStorage.flashget,
        'miniFlashget': localStorage.miniFlashget,
        'thunder': localStorage.thunder,
        'webThunder': localStorage.webThunder,
        'miniThunder': localStorage.miniThunder
    }, function(response) {
      // If content script is loaded
      if (response) {
        js = "";
      }
    });
    setTimeout(function() {
      eval(js);
      setPopupStatus();
    }, 500);
  });
}
  chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    switch(request.command) {
      case 'isUseExperimentalAPI':
        sendResponse({'useExperimentalAPI': useExperimentalAPI});
        break;
      case 'update':
        localStorage.contextmenu = (localStorage.contextmenu == "" ?
            'true': localStorage.contextmenu);
        sendResponse({
          "downloader": localStorage.downloader,
          "contextmenu": localStorage.contextmenu,
          "flashget": localStorage.flashget,
          "thunder": localStorage.thunder,
          'webThunder': localStorage.webThunder,
          'miniThunder': localStorage.miniThunder,
          'miniFlashget': localStorage.miniFlashget});
        break;
      case 'disable':
        localStorage.contextmenu = 'false';
        sendResponse(localStorage.contextmenu);
        break;
      case 'flashget':
      case 'miniFlashget':
        sendResponse(true);
        downloadLinkByFlashgetOrMiniFlashget(request.content, request.command);
        break;
      case 'thunder':
        sendResponse(true);
        downloadLinkByThunder(request.content);
        break;
      case 'webThunder':
        sendResponse(true);
        downloadLinkByWebThunder(request.content)
        break;
      case 'miniThunder':
        sendResponse(true);
        downloadLinkByMiniThunder(request.content);
        break;
      case 'copyLink':
        sendResponse(true);
        copyLinkToClipboard(request.content);
        break;
      default:
        sendResponse(false);
    }
  });
  
  chrome.extension.onConnect.addListener(function(port) {
    port.onMessage.addListener(function(message) {
      
      if (message) {
        switch (message.command) {
          case 'ByThunder':
            downloadAllLinksByThunder(message.content);
            break;
          case 'ByFlashget':
            downloadAllLinksByFlashget(message.content);
            break;
          case 'ByWebThunder':
            downloadAllLinksByWebThunder(message.content);
            break;
        }
      }
    });
  });

  function downloadAllLinksByFlashget(links) {
    var flashgetAgent = pluginobj.CreateObject('Flashget');
    var script_ = 'flashgetAgent.DownloadAll("",'
    for (var i = 0; i < links.length; i++) {
      script_ += '"' + links[i].url + '","' + links[i].text + '",';
    }
    script_ += '0)';
    eval(script_);
  }

  function downloadAllLinksByThunder(links) {
    var thunderAgent = pluginobj.CreateObject('ThunderAgent.Agent.1');
    for (var i = 0; i < links.length; i++) {
      thunderAgent.AddTask4(links[i].url, '', '', links[i].text, '', -1, 0, -1,
          '', '', '');
    }
    thunderAgent.CommitTasks2(1);
  }

  function downloadAllLinksByWebThunder(links) {
    var webThunderAgent = pluginobj.CreateObject('ThunderServer.WebThunder.1');
    var BatchId = webThunderAgent.BeginBatchTask();
    for (var i = 0; i < links.length; i++) {
      webThunderAgent.AddTaskToBatch(BatchId, links[i].url, links[i].text, '', '', '')
    }
    webThunderAgent.EndBatchTask(BatchId)
  }

  scanDownloader();
  setPopupStatus();
  if (useExperimentalAPI) {
    addMyItemToContextMenu();
  }
  localStorage.contextmenu = localStorage.contextmenu ? localStorage.contextmenu : 'true';

</script>
</body>
</html>
